package com.sdy.kzyy.pc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.StringUtil;
import com.sdy.kzyy.biz.dto.ManagerDTO;
import com.sdy.kzyy.biz.model.Manager;
import com.sdy.kzyy.biz.model.Store;
import com.sdy.kzyy.biz.service.ManagerService;
import com.sdy.kzyy.biz.service.StoreService;
import com.sdy.kzyy.pc.util.Constants;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.AbstractList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 王越洋
 * @since 2020-03-25
 */
@Slf4j
@RestController
@SuppressWarnings("unchecked")
@Api(tags = "账号管理")
@RequestMapping("/manager")
public class ManagerController extends BaseController {
    @Autowired
    private ManagerService managerService;
    @Autowired
    private StoreService storeService;

    @GetMapping("/pageData")
    @ApiOperation(value = "账号管理分页接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号"),
    })
    public Response pageData(HttpServletRequest request, String phone, Page<ManagerDTO> page) {

        Integer userId = getUserId(request, Constants.SESSION_WEB_USER_ID);
        Manager byId = managerService.getById(userId);
        if (byId == null) {
            Response.error("当前用户Session为空");
        }
        String storeCode = byId.getStoreCode();

        List<ManagerDTO> managerPage = managerService.getManagerPage(phone, storeCode, page);
        Integer managerPageSize = managerService.getManagerPageSize(phone, storeCode);
        IPage<ManagerDTO> storeVoPage = new Page<ManagerDTO>(page.getCurrent(), page.getSize(), managerPageSize).setRecords(managerPage);
        return Response.success(storeVoPage);
    }

    @GetMapping("/getById")
    @ApiOperation(value = "根据ID查询账号信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "账号ID"),
    })
    public Response get(Integer id) throws BizException {
        Assert.isNull(id, "参数为空");
        return Response.success(managerService.getById(id));
    }

    @PostMapping("/saveOne")
    @ApiOperation(value = "新增账号接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "manager", value = "用户实体类"),
    })
    public Response save(HttpServletRequest request,@RequestBody Manager manager) throws BizException {
        Assert.isTrue(!manager.getPhone().matches(Constants.REGEX_PHONE), "手机号格式错误");
        Assert.isBlank(manager.getPassword(), "密码不能为空");
        Integer userId = getUserId(request, Constants.SESSION_WEB_USER_ID);
        Assert.isNull(userId,"当前登录用户已失效");
        Manager byId = managerService.getById(userId);
        Assert.isNull(byId,"当前登录用户不存在");
        Assert.isTrue(!StringUtil.isBlank(byId.getStoreCode()),"你的账户不能新增用户 请联系管理员");
        manager.setState(1);
        Assert.isTrue(managerService.lambdaQuery().eq(Manager::getPhone, manager.getPhone()).count() > 0, "手机号已存在");
        return Response.success(managerService.save(manager));
    }


    @ApiOperation(value = "修改信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "manager", value = "用户实体类"),
    })
    @PostMapping("/updById")
    public Response updById(HttpServletRequest request,@RequestBody Manager manager) throws BizException {
        Assert.isNull(manager.getId(), "参数不能为空");
        Assert.isNull(manager.getName(), "名称不能为空");
        Integer userId = getUserId(request, Constants.SESSION_WEB_USER_ID);
        Assert.isNull(userId,"当前登录用户已失效");
        Manager byId = managerService.getById(userId);
        Assert.isNull(byId,"当前登录用户不存在");
        Assert.isTrue(!StringUtil.isBlank(byId.getStoreCode()),"你的账户不能修改网点信息 请联系管理员");
        managerService.updById(manager);
        return Response.success();
    }


    @ApiOperation(value = "修改状态接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "manager", value = "用户实体类"),
    })
    @PostMapping("/updStaById")
    public Response updStaById(HttpServletRequest request,@RequestBody Manager manager) throws BizException {
        Assert.isNull(manager.getId(), "参数不能为空");
        Assert.isNull(manager.getState(), "状态不能为空");
        Integer userId = getUserId(request, Constants.SESSION_WEB_USER_ID);
        Assert.isNull(userId,"当前登录用户已失效");
        Assert.isTrue(userId.equals(manager.getId()),"不能修改当前登录用户状态");
        managerService.updStaById(manager);
        return Response.success();
    }


    @ApiOperation(value = "修改密码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "manager", value = "用户实体类"),
    })
    @PostMapping("/updPwdById")
    public Response update(@RequestBody Manager manager) throws BizException {
        Assert.isNull(manager.getId(), "参数不能为空");
        Assert.isBlank(manager.getPassword(), "密码不能为空");
        managerService.updPwdById(manager);
        return Response.success();
    }


    @ApiOperation(value = "删除信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "manager", value = "用户实体类"),
    })
    @PostMapping("/delById")
    public Response delete(HttpServletRequest request,@RequestBody Manager manager) throws BizException {
        Assert.isNull(manager.getId(), "参数为空");
        Integer userId = getUserId(request, Constants.SESSION_WEB_USER_ID);
        Assert.isNull(userId,"当前登录用户已失效");
        Assert.isTrue(userId.equals(manager.getId()),"不能删除当前登录用户");
        return Response.success(managerService.removeById(manager.getId()));
    }

    @GetMapping("queryByCode")
    @ApiOperation(value = "查询区下所有药店")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "区号"),
    })
    public Response queryByCode(String code) {
        List<Store> list = storeService.lambdaQuery()
                .eq(code != null, Store::getAreaCode, code)
                .select(Store::getId, Store::getName, Store::getStoreCode, Store::getAreaCode)
                .list();
        return Response.success(list);
    }
}
